<?php
namespace Admin\Controller;
use Admin\Controller\MatApplyController;
use Admin\Controller\CheckController;
use Admin\Controller\ApplyController;
class ChaoDanController extends AdminController
{
	//抄单员管理
    protected function _initialize()
    {
		parent::_initialize ();
	}

	//抄单确认
	public function Confirm()
	{
//		$list=$this->getList('u.entry_no,u.user_name,u.identity_no,u.telephone,u.department_name,u.create_time,u.update_time,b.bank_short_name',
//				'user u','a.status=4 and (d.status=2 or d.status=99) and (u.status!=101 or u.status=9) and u.check_status=10','','left join app_bank b on u.bank_short_id=b.id left join app_advance a on u.id=a.user_id left join app_data_check d on u.id=d.user_id');

        $sql="select u.*,k.bank_short_name,m.status as data_status from `app_user` u left join `app_advance` b on u.id=b.user_id left join `app_data_check` m on m.user_id=u.id left join  `app_bank` k on u.loan_bank_id=k.id
		where b.status=4 and (m.status=2 or m.status=99) and (u.status!=101 or u.status=9) and u.check_status=10";
        $list=M()->query($sql);
        $this->assign('list',$list);
		$this->display();
	}

	//抄单员资料审核
	public function Confirm_check()
	{
		if(IS_POST)
		{
		  $user_id=$_POST['user_id'];
          $_POST['user_id']=$user_id;

          $_POST['add_time']=time();
	           	if($_POST['type']==0){
	                 $log=array('user_id'=>$user_id,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>5,'opinion'=>'抄单员资料已确认','note'=>'抄单员资料已确认','add_time'=>time(),'check_status'=>11);
	           	     M('check_log')->add($log);
	           	     $data=array('update_time'=>time(),'status'=>10,'check_status'=>11);
	           	     $check_status=M('user')->where('id='.$user_id)->save($data);
                     $_POST['status']=3;
	           	     $result=M('data_check')->where('user_id='.$user_id)->save($_POST);
	                 redirect('/Admin/ChaoDan/Confirm');
	           	}else{
	           		 $log=array('user_id'=>$user_id,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>5,'opinion'=>'抄单员资料待定','note'=>'抄单员资料待定','add_time'=>time(),'check_status'=>11);
		           	 M('check_log')->add($log);
		           	 $data=array('update_time'=>time(),'status'=>9);
		           	 $check_status=M('user')->where('id='.$user_id)->save($data);
		           	 $_POST['status']=2;
		           	 $result=M('data_check')->where('user_id='.$user_id)->save($_POST);
		             $this->error('抄单员审核待定',U('ChaoDan/Confirm'),0);
	           	}



		}else{
		    $id=I('get.user_id',0,'intval');
			$detail=new ApplyController();
            $detail->get_info($id);
	        $dataInfo=$this->getInfo('*','data_check','user_id='.$id);
	      $matInfo=$this->getInfo('a.*,b.name as pay_name',array('advance a','payee b'),'a.pay_id=b.id and a.user_id='.$id);
	      $userFiles = $this->getList('*','user_file','user_id = '.$id);

	      $this->assign('matInfo',$matInfo);
	      $this->assign('dataInfo',$dataInfo);
	      $this->assign('userFiles',$userFiles);
	      $this->display();

		}

	}

	//合同套打
	public function Prints()
	{
		/*$list=$this->data_check(1);
		$this->assign('list',$list);*/
		$info=new CheckController();
	    $list=$info->getCheck_status(11,'!=-1');
	    $this->assign('list',$list);
	    $this->display();
	}

	public function prints_detail()
	{
		if(IS_POST)
		{
		 	$nUserId = I('post.user_id','','intval');
			$_tmpUser= $this->getInfo('*','user','id = '.$nUserId);//用户信息

			if (is_null($_tmpUser))
			{
				$this->error('该用户不存在，请核实或联系管理员');
			}

			$home_area = $this->getAreaInfo($_tmpUser['home_area_id']);

			$_bank   = $this->getInfo('*','bank','id = '.$_tmpUser['loan_bank_id']);//银行规则

			switch ($_bank['bank_name'])
			{
				case '浙江诸暨农村商业银行股份有限公司':
					$file_name = 'zhuji.xls';
					break;

				case '中国建设银行股份有限公司杭州延安支行':
					$file_name = 'jianshe.xls';
					break;

				case '中国建设银行股份有限公司杭州高新支行':
					$file_name = 'jianshe.xls';
					break;

				case '杭州联合农村商业银行股份有限公司':
					$file_name = 'lianhe.xls';
					break;

				case '杭州银行股份有限公司清泰支行':
					$file_name = 'hangzhou.xls';
					break;

				case '浙江萧山农村商业股份有限公司城南支行':
					$file_name = 'xiaoshan.xls';
					break;

				default:
					break;
			}

			$_tmpCarInfo  = $this->getInfo('*','car_info','user_id = '.$nUserId);//车辆信息
			if (is_null($_tmpCarInfo))
			{
				$this->error('请先完善车辆信息');
			}
			$_bankRule    = $this->getInfo('*','bank_rule','bank_id = '.$_tmpUser['loan_bank_id']);//银行规则
			if (is_null($_bankRule))
			{
				$this->error('请联系管理员，维护银行规则');
			}
			$_car         = $this->getInfo('*','car','type_id = '.$_tmpCarInfo['car_form_id']);//车型
			if (is_null($_car))
			{
				$this->error('该用户选择的车型不存在');
			}
			$_advanceInfo = $this->getInfo('*','advance_info','user_id = '.$nUserId);//垫资信息
			if (is_null($_advanceInfo))
			{
				$this->error('请先完善垫资信息');
			}
	        set_time_limit(0);
		    ignore_user_abort(true);//检测用户断开
			$savePath = './Uploads/excel/';

	      	include VENDOR_PATH . 'PHPExcel/PHPExcel.php';

            //读取 写入
			$objReader = \PHPExcel_IOFactory::createReaderForFile($savePath . $file_name);
			$objPHPExcel = $objReader->load($savePath . $file_name);

			$objPHPExcel->setActiveSheetIndex(0);
			// 借款人信息
			$objPHPExcel->getActiveSheet()->setCellValue('G1',$_tmpUser['user_name']);//姓名
			$objPHPExcel->getActiveSheet()->setCellValue('J15',$_tmpUser['identity_no']);//身份证
			$objPHPExcel->getActiveSheet()->setCellValue('V15',$_tmpUser['telephone']);//电话
			$objPHPExcel->getActiveSheet()->setCellValue('AF15',$_tmpUser['sex'] == 1? '男' : '女');//性别
			$objPHPExcel->getActiveSheet()->setCellValue('AP15',$_tmpUser['marital_status']);//婚姻状况
			$objPHPExcel->getActiveSheet()->setCellValue('BE15',$_tmpUser['address']);//home_area_id 户籍所在地
			$objPHPExcel->getActiveSheet()->setCellValue('G16',$this->educationItems[$_tmpUser['education']]);//文化程度
			$objPHPExcel->getActiveSheet()->setCellValue('V16',$_tmpUser['company_tel']);//单位电话
			$objPHPExcel->getActiveSheet()->setCellValue('AF16',$this->companyNatureItems[$_tmpUser['company_nature']]);//单位性质
			$objPHPExcel->getActiveSheet()->setCellValue('AY16',$_tmpUser['company_office']);//单位职位
			$objPHPExcel->getActiveSheet()->setCellValue('G17',$_tmpUser['work_company']);//工作单位
			$objPHPExcel->getActiveSheet()->setCellValue('AF17',$_tmpUser['work_life']);//工作年限
			$objPHPExcel->getActiveSheet()->setCellValue('AY17',$_tmpUser['month_income']*12/10000);//年收入
			$objPHPExcel->getActiveSheet()->setCellValue('G18',$_tmpUser['now_address']);//现居住地
			// 配偶信息
			$objPHPExcel->getActiveSheet()->setCellValue('G21',$_tmpUser['spouse_name']);//配偶姓名
			$objPHPExcel->getActiveSheet()->setCellValue('J21',$_tmpUser['spouse_identity_no']);
			$objPHPExcel->getActiveSheet()->setCellValue('AF21','女');
			$objPHPExcel->getActiveSheet()->setCellValue('G22',$this->educationItems[$_tmpUser['spouse_education']]);
			$objPHPExcel->getActiveSheet()->setCellValue('BE21',$_tmpUser['spouse_address']);
			$objPHPExcel->getActiveSheet()->setCellValue('V21',$_tmpUser['spouse_telephone']);
			$objPHPExcel->getActiveSheet()->setCellValue('AY22',$_tmpUser['spouse_position']);
			$objPHPExcel->getActiveSheet()->setCellValue('AF22',$_tmpUser['spouse_company_nature']);
			$objPHPExcel->getActiveSheet()->setCellValue('G23',$_tmpUser['spouse_company']);
			// $objPHPExcel->getActiveSheet()->setCellValue('D9',$_tmpUser['spouse_company_address']);
			$objPHPExcel->getActiveSheet()->setCellValue('AF23',$_tmpUser['spouse_work_life']);
			$objPHPExcel->getActiveSheet()->setCellValue('AY23',$_tmpUser['spouse_month_income']*12/10000);
			// 车辆信息
			$objPHPExcel->getActiveSheet()->setCellValue('M2',$_tmpCarInfo['sale_price']);
			$objPHPExcel->getActiveSheet()->setCellValue('M4',$_tmpCarInfo['real_loan_quota']);
			$objPHPExcel->getActiveSheet()->setCellValue('G5',$_tmpCarInfo['term']/12);
			$objPHPExcel->getActiveSheet()->setCellValue('K5',$_tmpCarInfo['term']);
			$objPHPExcel->getActiveSheet()->setCellValue('AD5',$_tmpCarInfo['car_type']);
			$objPHPExcel->getActiveSheet()->setCellValue('M1',$home_area['province'].$home_area['city']);
			$objPHPExcel->getActiveSheet()->setCellValue('V1',$_car['type_name']);
			$objPHPExcel->getActiveSheet()->setCellValue('M8',$_tmpCarInfo['company_rate'] - $_bankRule['rate']*100);
			$objPHPExcel->getActiveSheet()->setCellValue('M10',$_bankRule['rate']*100);//银行手续费
			$objPHPExcel->getActiveSheet()->setCellValue('F14',$_advanceInfo['performance_fee']);
			$objPHPExcel->getActiveSheet()->setCellValue('F16',$_tmpCarInfo['license_area']);
			$objPHPExcel->getActiveSheet()->setCellValue('F17',$_tmpCarInfo['license_type'] == 1 ? '公牌' : '私牌');
			$objPHPExcel->getActiveSheet()->setCellValue('F19',$_tmpCarInfo['month_repayment']);
			$objPHPExcel->getActiveSheet()->setCellValue('BB1',$_tmpCarInfo['vin']);

			header("Pragma: public");
	        header("Expires: 0");
	        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
	        header("Content-Type:application/force-download");
	        header("Content-Type:application/vnd.ms-execl");
	        header("Content-Type:application/octet-stream");
	        header("Content-Type:application/download");;
	        header('Content-Disposition:attachment;filename='.$_tmpUser['user_name'].$file_name);
	        header("Content-Transfer-Encoding:binary");
            $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
//			$objWriter->save($_tmpUser['user_name'].$file_name);
	        $objWriter->save('php://output');
	        exit();
	    }


		$id=I('get.user_id',0,'intval');
		$detail=new ApplyController();
        $detail->get_info($id);

		$dataInfo=$this->getInfo('*','data_check','user_id='.$id);

		$matInfo=$this->getInfo('a.*,b.name as pay_name',array('advance a','payee b'),'a.pay_id=b.id and a.user_id='.$id);
		$advanceInfo=$this->getInfo('*','advance_info','user_id='.$id,'');
		$userFiles = $this->getList('*','user_file','user_id = '.$id,'id desc');
	    $this->assign('matInfo',$matInfo);
		$this->assign('dataInfo',$dataInfo);
		$this->assign('userFiles',$userFiles);
		$this->assign('advanceInfo',$advanceInfo);
		$this->display();
	}

	public function start_prints()
	{
		$user_id=I('post.user_id',0,'intval');
        $log=array('user_id'=>$user_id,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>'合同已套打','note'=>'合同已套打','add_time'=>time(),'check_status'=>12);
       	M('check_log')->add($log);
       	$data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'check_status'=>12);
        M('user')->where('id='.$user_id)->save($data);
        $result=M('data_check')->where('user_id='.$user_id)->setField('status',4);
        redirect('/Admin/ChaoDan/Prints');
	}

	//抄单资料审核(内勤主管)
	public function Check()
	{
		$info=new CheckController();
	    $list=$info->getCheck_status(12,'!=0');
	    $this->assign('list',$list);
		// $list=$this->data_check(2);
		// $this->assign('list',$list);
		$this->display();
	}

//抄单资料审核详情(内勤主管)
   public function Check_Info()
   {
	   	if(IS_POST)
	   	{
	   		  $user=$this->getInfo('*','user','id='.$_POST['user_id'].' and check_status=13');
	   		  if($user){
	   		  	 M('user')->where('id='.$_POST['user_id'])->setField('status',10);
	   		  	 $this->error('该申请已审核');
	   		  }
		   		  	 if($_POST['type']==5){
		   		  	    $log=array('user_id'=>$_POST['user_id'],'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>1,'opinion'=>'内勤主管审核通过','note'=>'内勤主管审核通过','add_time'=>time(),'check_status'=>13);
	                    M('check_log')->add($log);
	                    $data=array('status'=>10,'check_status'=>13,'update_time'=>time());
		   		  	    $result=M('user')->where('id='.$_POST['user_id'])->save($data);
		   		  	    M('data_check')->where('user_id='.$_POST['user_id'])->setField('status',$_POST['type']);
		   		  	    redirect('/Admin/ChaoDan/Check');
	                    exit;
		   		  	 }else{
		   		  	 	M('data_check')->where('user_id='.$_POST['user_id'])->setField('status',$_POST['type']);
		   		  	 	M('check_log')->where('status=5 and user_id='.$_POST['user_id'])->setField('update_status',0);
		   		  	 	$data=array('update_time'=>time(),'check_admin_id'=>session('adminInfo.id'),'check_status'=>10,'status'=>10);
		   		  	 	M('user')->where('id='.$_POST['user_id'])->setField($data);
		   		  	 	$this->error('审核不通过',U('ChaoDan/Check'));
		   		  	 	exit;
		   		  	 }

	   	}else{
	   		 $id=I('get.user_id',0,'intval');
			 $detail=new ApplyController();
	           $detail->get_info($id);
		      $dataInfo=$this->getInfo('*','data_check','user_id='.$id);

		      $matInfo=$this->getInfo('a.*,b.name as pay_name',array('advance a','payee b'),'a.pay_id=b.id and a.user_id='.$id);
		      $userFiles = $this->getList('*','user_file','user_id = '.$id);

		      $this->assign('matInfo',$matInfo);

		      $this->assign('dataInfo',$dataInfo);
		      $this->assign('userFiles',$userFiles);
		      $this->display();
	   	}



   }

	//抄单审核资料详情
	public function data_check($data_status=0)
	{
        $where = '1 = 1';
	    if(I('get.aid', -1, 'intval') <> -1)
	    {
	      if(I('get.aid', -1, 'intval')==0){
	        $where .=' and  u.aid =0';
	      }else{
	        $where .=' and aid='.I('get.aid', 0, 'intval');
	      }
	    }
	    $where .= I('get.user_name') != '' ? ' and u.user_name like "%'.I('get.user_name').'%"' : '';

	    $where .= I('get.telephone') != '' ? ' and u.telephone = '.I('get.telephone') : '';

	    $where .= I('get.create_time_beg') != '' ? ' and u.create_time >= '.strtotime(I('get.create_time_beg')) : '';

	    $where .= I('get.create_time_end') != '' ? ' and u.create_time < '.strtotime(I('get.create_time_end')) : '';

	    $where .= I('get.status') != '' ? ' and d.status='.I('get.status') : ' and d.status='.$data_status;

	    $where.=' and u.id=d.user_id and u.check_status=6 and u.status!=101';
		$list=$this->getList('d.status as datastatus,u.*',array('user u','data_check d'),$where,'id desc',true);
		foreach ($list as $key => $value) {
			 $group=$this->getInfo('*','group','id='.$value['gpid']);
             $list[$key]['group_name']=$group['name'];
		}
		return $list;
	}

	//抄单审核中
	public function setData_check()
	{
	 	if($_POST['type']==1){
	 		$info=$this->getInfo('*','user','check_admin_id='.session('adminInfo.id').' and check_status>=6 and status=101');
			if($info){
	          echo '还有未审核完的申请';
	          exit;
			}
			
          $data_check=$this->getInfo('*','data_check','user_id='.$_POST['id'].' and status='.$_POST['data_status']);
          if($data_check){
            $result=M('user')->where('id='.$_POST['id'])->setField('status',101);
			if($result){
				 M('user')->where('id='.$_POST['id'])->setField('check_admin_id',session('adminInfo.id'));
	             $this->ajaxReturn(array('info'=>'操作状态设置成功','status'=>1));
			}else{
	             $this->ajaxReturn(array('info'=>'该申请正在操作中,或不在当前审核下','status'=>0));
			}
		  }else{
		  	$this->ajaxReturn(array('info'=>'参数错误','status'=>0));
		  }
	 	}else{
	 		$info=$this->getInfo('*','user','check_admin_id='.session('adminInfo.id').' and status=101');
			if($info){
	          echo '还有未审核完的申请';
	          exit;
			}
	 	   $data_check=$this->getInfo('*','data_check','user_id='.$_POST['id']);
	 	   		if(!$data_check){
            $result=M('user')->where('id='.$_POST['id'])->setField('status',101);
			if($result){
				 M('user')->where('id='.$_POST['id'])->setField('check_admin_id',session('adminInfo.id'));
	             $this->ajaxReturn(array('info'=>'操作状态设置成功','status'=>1));
			}else{
	              $this->ajaxReturn(array('info'=>'该申请正在操作中,或不在当前审核下','status'=>0));
			}
			}else{
	           $this->ajaxReturn(array('info'=>'该抄单审核已完成','status'=>0));
			}
	 	}


	}

}